#pragma once
#include <iostream>

template<class T>
class unique_ptr
{
public:
    unique_ptr(T* ptr = nullptr)
    :_ptr(ptr)
    {
        std::cout << "unique_ptr" << std::endl;
    }

    // 析构时释放资源
    ~unique_ptr()
    {
        if(_ptr != nullptr)
        {
            delete _ptr;
            _ptr = nullptr;
        }
        std::cout << " ~unique_ptr" << std::endl;
    }

    T& operator*()
    {
        return *_ptr;
    }

    T* operator->()
    {
        return _ptr;
    }

    T*& get()
    {
        return _ptr;
    }

    // 赋值，资源只能被一个auto_ptr指向，p2 = p1
    unique_ptr<T>& operator=(unique_ptr<T>& p1) = delete;

    // 拷贝构造p2(p1)
    unique_ptr(unique_ptr<T>& p1) = delete;

private:
    T* _ptr; // 指针
};